#include "MLTrajectorySaver.h"
#include "MLDataPoint.h"

CMLTrajectorySaver::CMLTrajectorySaver(void)
{
}

CMLTrajectorySaver::~CMLTrajectorySaver(void)
{
	if (m_FileStream.is_open())
		m_FileStream.close();
}

int CMLTrajectorySaver::Initialize(const std::wstring& wstrFileName)
{
	m_FileStream.open(wstrFileName.c_str());
	if (m_FileStream.fail())
	{
		ML_ASSERT(false);
		return -1;
	}
	return 0;
}

int CMLTrajectorySaver::Save(const CMLTrajectoryCollection& trajs,int startindex/*= -1*/, int endindex/*= -1*/)
{
	if (startindex < 0) startindex = 0;
	if (endindex < 0)  endindex = trajs.Count();
	int i;
	for (i=startindex; i<endindex; i++)
	{
		const CMLTrajectory& traj = trajs.Get(i);
		for (int p=0;p<traj.GetPointCount(); p++)
		{
			m_FileStream << traj.GetName() << L'\t' << traj.GetPoint(p).ToWString(L'\t') << L'\n';
		}
	}
	return 0;
}

void CMLTrajectorySaver::Close()
{
	m_FileStream.close();
}

